﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using jnxc.DBUtility;//Please add references
namespace jnxc.DAL
{
	/// <summary>
	/// 数据访问类:routineRecord
	/// </summary>
	public partial class routineRecord
	{
		public routineRecord()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("routinId", "routineRecord"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int routinId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from routineRecord");
			strSql.Append(" where routinId=@routinId");
			SqlParameter[] parameters = {
					new SqlParameter("@routinId", SqlDbType.Int,4)
			};
			parameters[0].Value = routinId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(jnxc.Model.routineRecord model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into routineRecord(");
			strSql.Append("mhid,mpid,harmParts,incomingArea,incomingWay,findTime,isStandard,sid,standardBH,standardArea,representArea,damageArea,damageLevel,isDisaster,disasterArea,investigationName,investigationTime)");
			strSql.Append(" values (");
			strSql.Append("@mhid,@mpid,@harmParts,@incomingArea,@incomingWay,@findTime,@isStandard,@sid,@standardBH,@standardArea,@representArea,@damageArea,@damageLevel,@isDisaster,@disasterArea,@investigationName,@investigationTime)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@mhid", SqlDbType.Int,4),
					new SqlParameter("@mpid", SqlDbType.Int,4),
					new SqlParameter("@harmParts", SqlDbType.NVarChar,50),
					new SqlParameter("@incomingArea", SqlDbType.NVarChar,50),
					new SqlParameter("@incomingWay", SqlDbType.NVarChar,50),
					new SqlParameter("@findTime", SqlDbType.DateTime),
					new SqlParameter("@isStandard", SqlDbType.Bit,1),
					new SqlParameter("@sid", SqlDbType.Int,4),
					new SqlParameter("@standardBH", SqlDbType.VarChar,20),
					new SqlParameter("@standardArea", SqlDbType.Decimal,9),
					new SqlParameter("@representArea", SqlDbType.Decimal,9),
					new SqlParameter("@damageArea", SqlDbType.Decimal,9),
					new SqlParameter("@damageLevel", SqlDbType.NVarChar,50),
					new SqlParameter("@isDisaster", SqlDbType.Bit,1),
					new SqlParameter("@disasterArea", SqlDbType.Decimal,9),
					new SqlParameter("@investigationName", SqlDbType.NVarChar,20),
					new SqlParameter("@investigationTime", SqlDbType.DateTime)};
			parameters[0].Value = model.mhid;
			parameters[1].Value = model.mpid;
			parameters[2].Value = model.harmParts;
			parameters[3].Value = model.incomingArea;
			parameters[4].Value = model.incomingWay;
			parameters[5].Value = model.findTime;
			parameters[6].Value = model.isStandard;
			parameters[7].Value = model.sid;
			parameters[8].Value = model.standardBH;
			parameters[9].Value = model.standardArea;
			parameters[10].Value = model.representArea;
			parameters[11].Value = model.damageArea;
			parameters[12].Value = model.damageLevel;
			parameters[13].Value = model.isDisaster;
			parameters[14].Value = model.disasterArea;
			parameters[15].Value = model.investigationName;
			parameters[16].Value = model.investigationTime;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(jnxc.Model.routineRecord model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update routineRecord set ");
			strSql.Append("mhid=@mhid,");
			strSql.Append("mpid=@mpid,");
			strSql.Append("harmParts=@harmParts,");
			strSql.Append("incomingArea=@incomingArea,");
			strSql.Append("incomingWay=@incomingWay,");
			strSql.Append("findTime=@findTime,");
			strSql.Append("isStandard=@isStandard,");
			strSql.Append("sid=@sid,");
			strSql.Append("standardBH=@standardBH,");
			strSql.Append("standardArea=@standardArea,");
			strSql.Append("representArea=@representArea,");
			strSql.Append("damageArea=@damageArea,");
			strSql.Append("damageLevel=@damageLevel,");
			strSql.Append("isDisaster=@isDisaster,");
			strSql.Append("disasterArea=@disasterArea,");
			strSql.Append("investigationName=@investigationName,");
			strSql.Append("investigationTime=@investigationTime");
			strSql.Append(" where routinId=@routinId");
			SqlParameter[] parameters = {
					new SqlParameter("@mhid", SqlDbType.Int,4),
					new SqlParameter("@mpid", SqlDbType.Int,4),
					new SqlParameter("@harmParts", SqlDbType.NVarChar,50),
					new SqlParameter("@incomingArea", SqlDbType.NVarChar,50),
					new SqlParameter("@incomingWay", SqlDbType.NVarChar,50),
					new SqlParameter("@findTime", SqlDbType.DateTime),
					new SqlParameter("@isStandard", SqlDbType.Bit,1),
					new SqlParameter("@sid", SqlDbType.Int,4),
					new SqlParameter("@standardBH", SqlDbType.VarChar,20),
					new SqlParameter("@standardArea", SqlDbType.Decimal,9),
					new SqlParameter("@representArea", SqlDbType.Decimal,9),
					new SqlParameter("@damageArea", SqlDbType.Decimal,9),
					new SqlParameter("@damageLevel", SqlDbType.NVarChar,50),
					new SqlParameter("@isDisaster", SqlDbType.Bit,1),
					new SqlParameter("@disasterArea", SqlDbType.Decimal,9),
					new SqlParameter("@investigationName", SqlDbType.NVarChar,20),
					new SqlParameter("@investigationTime", SqlDbType.DateTime),
					new SqlParameter("@routinId", SqlDbType.Int,4)};
			parameters[0].Value = model.mhid;
			parameters[1].Value = model.mpid;
			parameters[2].Value = model.harmParts;
			parameters[3].Value = model.incomingArea;
			parameters[4].Value = model.incomingWay;
			parameters[5].Value = model.findTime;
			parameters[6].Value = model.isStandard;
			parameters[7].Value = model.sid;
			parameters[8].Value = model.standardBH;
			parameters[9].Value = model.standardArea;
			parameters[10].Value = model.representArea;
			parameters[11].Value = model.damageArea;
			parameters[12].Value = model.damageLevel;
			parameters[13].Value = model.isDisaster;
			parameters[14].Value = model.disasterArea;
			parameters[15].Value = model.investigationName;
			parameters[16].Value = model.investigationTime;
			parameters[17].Value = model.routinId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int routinId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from routineRecord ");
			strSql.Append(" where routinId=@routinId");
			SqlParameter[] parameters = {
					new SqlParameter("@routinId", SqlDbType.Int,4)
			};
			parameters[0].Value = routinId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string routinIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from routineRecord ");
			strSql.Append(" where routinId in ("+routinIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public jnxc.Model.routineRecord GetModel(int routinId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 routinId,mhid,mpid,harmParts,incomingArea,incomingWay,findTime,isStandard,sid,standardBH,standardArea,representArea,damageArea,damageLevel,isDisaster,disasterArea,investigationName,investigationTime from routineRecord ");
			strSql.Append(" where routinId=@routinId");
			SqlParameter[] parameters = {
					new SqlParameter("@routinId", SqlDbType.Int,4)
			};
			parameters[0].Value = routinId;

			jnxc.Model.routineRecord model=new jnxc.Model.routineRecord();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public jnxc.Model.routineRecord DataRowToModel(DataRow row)
		{
			jnxc.Model.routineRecord model=new jnxc.Model.routineRecord();
			if (row != null)
			{
				if(row["routinId"]!=null && row["routinId"].ToString()!="")
				{
					model.routinId=int.Parse(row["routinId"].ToString());
				}
				if(row["mhid"]!=null && row["mhid"].ToString()!="")
				{
					model.mhid=int.Parse(row["mhid"].ToString());
				}
				if(row["mpid"]!=null && row["mpid"].ToString()!="")
				{
					model.mpid=int.Parse(row["mpid"].ToString());
				}
				if(row["harmParts"]!=null)
				{
					model.harmParts=row["harmParts"].ToString();
				}
				if(row["incomingArea"]!=null)
				{
					model.incomingArea=row["incomingArea"].ToString();
				}
				if(row["incomingWay"]!=null)
				{
					model.incomingWay=row["incomingWay"].ToString();
				}
				if(row["findTime"]!=null && row["findTime"].ToString()!="")
				{
					model.findTime=DateTime.Parse(row["findTime"].ToString());
				}
				if(row["isStandard"]!=null && row["isStandard"].ToString()!="")
				{
					if((row["isStandard"].ToString()=="1")||(row["isStandard"].ToString().ToLower()=="true"))
					{
						model.isStandard=true;
					}
					else
					{
						model.isStandard=false;
					}
				}
				if(row["sid"]!=null && row["sid"].ToString()!="")
				{
					model.sid=int.Parse(row["sid"].ToString());
				}
				if(row["standardBH"]!=null)
				{
					model.standardBH=row["standardBH"].ToString();
				}
				if(row["standardArea"]!=null && row["standardArea"].ToString()!="")
				{
					model.standardArea=decimal.Parse(row["standardArea"].ToString());
				}
				if(row["representArea"]!=null && row["representArea"].ToString()!="")
				{
					model.representArea=decimal.Parse(row["representArea"].ToString());
				}
				if(row["damageArea"]!=null && row["damageArea"].ToString()!="")
				{
					model.damageArea=decimal.Parse(row["damageArea"].ToString());
				}
				if(row["damageLevel"]!=null)
				{
					model.damageLevel=row["damageLevel"].ToString();
				}
				if(row["isDisaster"]!=null && row["isDisaster"].ToString()!="")
				{
					if((row["isDisaster"].ToString()=="1")||(row["isDisaster"].ToString().ToLower()=="true"))
					{
						model.isDisaster=true;
					}
					else
					{
						model.isDisaster=false;
					}
				}
				if(row["disasterArea"]!=null && row["disasterArea"].ToString()!="")
				{
					model.disasterArea=decimal.Parse(row["disasterArea"].ToString());
				}
				if(row["investigationName"]!=null)
				{
					model.investigationName=row["investigationName"].ToString();
				}
				if(row["investigationTime"]!=null && row["investigationTime"].ToString()!="")
				{
					model.investigationTime=DateTime.Parse(row["investigationTime"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select routinId,mhid,mpid,harmParts,incomingArea,incomingWay,findTime,isStandard,sid,standardBH,standardArea,representArea,damageArea,damageLevel,isDisaster,disasterArea,investigationName,investigationTime ");
			strSql.Append(" FROM routineRecord ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
        public DataSet GetListV(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM VroutineRecord ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" routinId,mhid,mpid,harmParts,incomingArea,incomingWay,findTime,isStandard,sid,standardBH,standardArea,representArea,damageArea,damageLevel,isDisaster,disasterArea,investigationName,investigationTime ");
			strSql.Append(" FROM routineRecord ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM VroutineRecord ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
        public int GetRecordCountdis(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM (SELECT DISTINCT pname, pbh, cname, cbh, coname, cobh, tname, tbh, tcbh FROM dbo.VroutineRecord ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(")  T");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.routinId desc");
			}
			strSql.Append(")AS Row, T.*  from VroutineRecord T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}
        

        public DataSet GetListByPagedis(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.tcbh desc");
            }
            strSql.Append(")AS Row, T.*  from (SELECT DISTINCT pname, pbh, cname, cbh, coname, cobh, tname, tbh, tcbh,tcxlid FROM dbo.VroutineRecord ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(")  T ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
        public int GetRecordCountbdis(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM (SELECT DISTINCT pname, pbh, cname, cbh, coname, cobh, tname, tbh, tcbh,standardBH,xbhbh,jd,wd,hb,standardArea,representArea FROM dbo.VroutineRecord ");
            if (strWhere.Trim() != "")
            {
                strSql.Append("where "+strWhere);
            }
            strSql.Append(")  T");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        public DataSet GetListByPagebdis(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.xbhbh desc");
            }
            strSql.Append(")AS Row, T.*  from (SELECT DISTINCT pname, pbh, cname, cbh, coname, cobh, tname, tbh, tcbh,standardBH,xbhbh,xbhid,jd,wd,hb,standardArea,representArea FROM dbo.VroutineRecord  ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" where "+strWhere);
            }
            strSql.Append(")  T ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

